new Vue({
    el: '#app',
    data() {
        return {
            goodsPage: {}, // 商品的分页信息含有商品集合
            stockUpdate: {
                gid: 0,
                gname: '',
                num: 0,
                desc: '',
                type: 1,
                typeStr: '入库',
                errorMsg: ''
            },
            provs: [],
            search: {
                goodsName: '',
                proId: 0
            }
        }
    },
    created() {
        this.loadGoodsData(1)
        this.loadProvData()
    },
    methods: {
        loadGoodsData(page) {
            let _this = this;
            axios.get('http://localhost:8051/goods/' + page)
                .then((response) => {
                    _this.goodsPage = response.data.data;
                });

            this.stockUpdate.gid = sessionStorage.getItem('gid');
            this.stockUpdate.gname = sessionStorage.getItem('gname');
        },
        toStockUpdate(gid, gname) {
            sessionStorage.setItem('gid', gid);
            sessionStorage.setItem('gname', gname);
            location.href = 'stockupdate.html';
        },
        changeType(t) {
            this.stockUpdate.type = t;
            this.stockUpdate.typeStr = t === 1 ? '入库' : '出库';
        },
        updateStock() {
            let _this = this;
            let n = this.stockUpdate.type === 1 ? (this.stockUpdate.num) : (0 - this.stockUpdate.num);
            console.log('n:' + n)
            axios.put('http://localhost:8061/stock/' + _this.stockUpdate.gid + '/' + n)
                .then((response) => {
                    if (response.data.success) {
                        location.href = 'goods.html';
                    } else {
                        _this.stockUpdate.errorMsg = response.data.data;
                    }

                });
        },
        loadProvData() {
            let _this = this;
            axios.get('http://localhost:8063/provider')
                .then((response) => {
                    _this.provs = response.data.data;
                });
        },
        searchGoods() {
            console.log('供应商名称：' + this.search.proId)
            console.log('商品名称：' + this.search.goodsName)
            let gn = this.search.goodsName + '    ';
            let _this = this;
            axios.get('http://localhost:8051/goods/' + gn + '/' + this.search.proId)
                .then((response) => {
                    _this.goodsPage = response.data.data;
                    console.log(_this.goodsPage)
                });
        }
    }
});